package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.dao.SetmealDao;
import cn.itcast.pojo.*;
import cn.itcast.service.ReportBusinessService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import java.text.SimpleDateFormat;
import java.util.*;

@Service(version = "1.0.0")
@Slf4j
public class ReportBusinessServiceImpl implements ReportBusinessService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetmealDao setmealDao;


    @Override
    public Map<String,Object> reportBusiness(String today, String startWeek, String endWeek, String month) {
        ReportData reportData=new ReportData();
        //设置今天的日期
        reportData.setReportDate(today);

        //查询今日是否有新注册会员
        List<Member> members = this.memberDao.selectHaveNew(today);
        //如果集合不为空 添加属性到今日新增会员数中
        if(null!=members){
            reportData.setTodayNewMember(members.size());
        }else {
            reportData.setTodayNewMember(0);
        }

        //查询总会员数
        Integer total = this.memberDao.selectMemberTotal();
        //设置总会员数
        reportData.setTotalMember(total);

        //查询本周是否有新增会员数
        List<Member> members1 = this.memberDao.selectHaveNewInWeek(startWeek, endWeek);
        if(null!=members1){
            //设置本周新增会员数
            reportData.setThisWeekNewMember(members1.size());
        }else {
            reportData.setThisWeekNewMember(0);
        }

        //获得本月新增会员数
        List<Member> members2 = this.memberDao.selectNewMemberMonth(month + "-1", month + "-31");
        if(null!=members2){
            //设置本月新增会员数
            reportData.setThisMonthNewMember(members2.size());
        }else {
            reportData.setThisMonthNewMember(0);
        }

        //查询该日预约数
        List<Order> orders = this.orderDao.selectCountToday(today);
        if(null!=orders){
            //设置该日预约数
            reportData.setTodayOrderNumber(orders.size());
        }else {
            reportData.setTodayOrderNumber(0);
        }

        //查询该日到诊数
        List<Order> orders1 = this.orderDao.selectComeCountToday(today);
        if(null!=orders1){
            //设置该日到诊数
            reportData.setTodayVisitsNumber(orders1.size());
        }else {
            reportData.setTodayVisitsNumber(0);
        }

        //查询该周预约数
        List<Order> orders2 = this.orderDao.selectCountWeek(startWeek, endWeek);
        if(null!=orders2){
            //设置该周到诊数
            reportData.setThisWeekOrderNumber(orders2.size());
        }else {
            reportData.setThisWeekOrderNumber(0);
        }


        //查询该周到诊数
        List<Order> orders3 = this.orderDao.selectComeCountWeek(month + "-1", month + "-31");
        if(null!=orders3){
            //设置该周到诊数
            reportData.setThisWeekVisitsNumber(orders3.size());
        }else {
            reportData.setThisWeekVisitsNumber(0);
        }

        //查询该月预约数
        List<Order> orders4 = this.orderDao.selectCountWeek(month + "-1", month + "-31");
        if(null!=orders4){
            //设置该月预约数
            reportData.setThisMonthOrderNumber(orders4.size());
        }else {
            reportData.setThisMonthOrderNumber(0);
        }

        //查询该月到诊数
        List<Order> orders5 = this.orderDao.selectComeCountWeek(month + "-1", month + "-31");
        if(null!=orders5){
            //设置该月到诊数
            reportData.setThisMonthVisitsNumber(orders5.size());
        }else {
            reportData.setThisMonthVisitsNumber(0);
        }

        //设置前端需要的参数
        Map<String,Object> result=new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",reportData.getTodayNewMember());
        result.put("totalMember",reportData.getTotalMember());
        result.put("thisWeekNewMember",reportData.getThisWeekNewMember());
        result.put("thisMonthNewMember",reportData.getThisMonthNewMember());
        result.put("todayOrderNumber",reportData.getTodayOrderNumber());
        result.put("thisWeekOrderNumber",reportData.getThisWeekOrderNumber());
        result.put("thisMonthOrderNumber",reportData.getThisMonthOrderNumber());
        result.put("todayVisitsNumber",reportData.getTodayVisitsNumber());
        result.put("thisWeekVisitsNumber",reportData.getThisWeekVisitsNumber());
        result.put("thisMonthVisitsNumber",reportData.getThisMonthVisitsNumber());

        //查询热门套餐
        List<Map> hotSetmeal = this.orderDao.findHotSetmeal();
        log.info("List集合为:{}",hotSetmeal);
        result.put("hotSetmeal",hotSetmeal);

        return result;
    }



    @Override
    public Map<String,Object> reportBusiness1(){
        //获取今天的日期
        Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();
        //根据当前日期获取所在周的开始日期
        Date startWeekDay = DateUtils.getFirstDayOfWeek(today);
        //根据当前日期获取所在周结束日期
        Date endWeekDay = DateUtils.getLastDayOfWeek(today);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        //今日日期
        String todayDate = sdf.format(today);
        log.info("今日日期:{}",todayDate);
        //本周开始时间
        String startWeek = sdf.format(startWeekDay);
        log.info("本周开始时间:{}",startWeek);
        //本周结束时间
        String endWeek = sdf.format(endWeekDay);
        log.info("本周结束时间:{}",endWeek);
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM");
        //本月时间
        String month = sdf1.format(today);
        log.info("本月时间:{}",month);
        ReportData reportData=new ReportData();
        //设置今天的日期
        reportData.setReportDate(todayDate);

        //查询今日是否有新注册会员
        List<Member> members = this.memberDao.selectHaveNew(todayDate);
        //如果集合不为空 添加属性到今日新增会员数中
        if(null!=members){
            reportData.setTodayNewMember(members.size());
        }else {
            reportData.setTodayNewMember(0);
        }

        //查询总会员数
        Integer total = this.memberDao.selectMemberTotal();
        //设置总会员数
        reportData.setTotalMember(total);

        //查询本周是否有新增会员数
        List<Member> members1 = this.memberDao.selectHaveNewInWeek(startWeek, endWeek);
        if(null!=members1){
            //设置本周新增会员数
            reportData.setThisWeekNewMember(members1.size());
        }else {
            reportData.setThisWeekNewMember(0);
        }

        //获得本月新增会员数
        List<Member> members2 = this.memberDao.selectNewMemberMonth(month + "-1", month + "-31");
        if(null!=members2){
            //设置本月新增会员数
            reportData.setThisMonthNewMember(members2.size());
        }else {
            reportData.setThisMonthNewMember(0);
        }

        //查询该日预约数
        List<Order> orders = this.orderDao.selectCountToday(todayDate);
        if(null!=orders){
            //设置该日预约数
            reportData.setTodayOrderNumber(orders.size());
        }else {
            reportData.setTodayOrderNumber(0);
        }

        //查询该日到诊数
        List<Order> orders1 = this.orderDao.selectComeCountToday(todayDate);
        if(null!=orders1){
            //设置该日到诊数
            reportData.setTodayVisitsNumber(orders1.size());
        }else {
            reportData.setTodayVisitsNumber(0);
        }

        //查询该周预约数
        List<Order> orders2 = this.orderDao.selectCountWeek(startWeek, endWeek);
        if(null!=orders2){
            //设置该周到诊数
            reportData.setThisWeekOrderNumber(orders2.size());
        }else {
            reportData.setThisWeekOrderNumber(0);
        }


        //查询该周到诊数
        List<Order> orders3 = this.orderDao.selectComeCountWeek(month + "-1", month + "-31");
        if(null!=orders3){
            //设置该周到诊数
            reportData.setThisWeekVisitsNumber(orders3.size());
        }else {
            reportData.setThisWeekVisitsNumber(0);
        }

        //查询该月预约数
        List<Order> orders4 = this.orderDao.selectCountWeek(month + "-1", month + "-31");
        if(null!=orders4){
            //设置该月预约数
            reportData.setThisMonthOrderNumber(orders4.size());
        }else {
            reportData.setThisMonthOrderNumber(0);
        }

        //查询该月到诊数
        List<Order> orders5 = this.orderDao.selectComeCountWeek(month + "-1", month + "-31");
        if(null!=orders5){
            //设置该月到诊数
            reportData.setThisMonthVisitsNumber(orders5.size());
        }else {
            reportData.setThisMonthVisitsNumber(0);
        }

        //设置前端需要的参数
        Map<String,Object> result=new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",reportData.getTodayNewMember());
        result.put("totalMember",reportData.getTotalMember());
        result.put("thisWeekNewMember",reportData.getThisWeekNewMember());
        result.put("thisMonthNewMember",reportData.getThisMonthNewMember());
        result.put("todayOrderNumber",reportData.getTodayOrderNumber());
        result.put("thisWeekOrderNumber",reportData.getThisWeekOrderNumber());
        result.put("thisMonthOrderNumber",reportData.getThisMonthOrderNumber());
        result.put("todayVisitsNumber",reportData.getTodayVisitsNumber());
        result.put("thisWeekVisitsNumber",reportData.getThisWeekVisitsNumber());
        result.put("thisMonthVisitsNumber",reportData.getThisMonthVisitsNumber());

        //查询热门套餐前四
        List<Map> hotSetmeal = this.orderDao.findHotSetmeal();
        log.info("List集合为:{}",hotSetmeal);
        result.put("hotSetmeal",hotSetmeal);

        return result;


    }
}
